//package com.example.restaurant.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.example.restaurant.common.BaseContext;
//import com.example.restaurant.common.R;
//import com.example.restaurant.entity.Employee;
//import com.example.restaurant.entity.User;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.util.AntPathMatcher;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///*
//       拦截器 拦截未登录用户
// */
//
//@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
//@Slf4j
//public class LoginCheckFilter implements Filter {
//
//    //路径匹配器，支持通配符
//    public  static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        log.info("拦截到请求： {}",request.getRequestURI());
//        //1.获取请求的url
//        String requestURI = request.getRequestURI();
//        //2.判断本次请求是否需要处理
//        //放行的路径
//        String[] urls = new String[]{
//          "/employee/login",
//          "/employee/logout",
//           "/backend/**",
//           "/front/**",
//            "/user/login"
//        };
//
//        Boolean check = false;
//        for (String url : urls) {
//            if (PATH_MATCHER.match(url,requestURI)){
//                check = true;
//            }
//        }
//        //3.如果不需要处理则放行
//        if(check){
//            filterChain.doFilter(request,response);
//            return;
//        }
//        //4、判断登录状态，若已登录，则放行
//        if (request.getSession().getAttribute("employee")!=null){
//            Employee employee = (Employee) request.getSession().getAttribute("employee");
//            BaseContext.setId(employee.getId());
//            filterChain.doFilter(request,response);
//            return;
//        }
//        //4、判断登录状态，若已登录，则放行
//        if (request.getSession().getAttribute("user")!=null){
//            User user = (User) request.getSession().getAttribute("user");
//            BaseContext.setId(user.getId());
//            filterChain.doFilter(request,response);
//            return;
//        }
//        //5.如果没有登录信息，则通过输出流的方式向页面响应数据
//        log.info(R.error("NOTLOGIN").toString());
//        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//    }
//}
